package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.LocaleManager;
import ge.telasi.tasks.ejb.TaskFacadeRemote;
import ge.telasi.tasks.model.Group;
import ge.telasi.tasks.ui.comp.ListModel;
import ge.telasi.tasks.ui.comp.ListUtils;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComponent;
import javax.swing.JList;
import static ge.telasi.tasks.ui.log.LoggerUtils.clearLogger;

/**
 * @author dimitri
 */
public class GroupList extends JList implements Loggable {
    private Logger logger;

    public GroupList() {
        setModel(new GroupListModel());
        setCellRenderer(new GroupRenderer());
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1 && getSelectedGroup() != null) {
                    Group group = getSelectedGroup();
                    GroupPropertiesDialog dialog = new GroupPropertiesDialog(GroupList.this);
                    dialog.setGroup(group);
                    UIUtils.openDialog(dialog);
                }
            }
        });
    }

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    @Override
    public JComponent[] allFields() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void display(List<Group> groups) {
        GroupListModel model = (GroupListModel) getModel();
        model.setData(groups);
    }

    public boolean refresh() {
        clearSelection();
        if (Platform.getDefault().validateConnection()) {
            clearLogger(getLogger(), null);
            try {
                Platform pl = Platform.getDefault();
                TaskFacadeRemote facade = pl.getFacade();
                List<Group> groups = facade.getAllGroups(pl.getCredentials());
                display(groups);
                return true;
            } catch (Exception ex) {
                LoggerUtils.manageException(this, getLogger(), ex);
                return false;
            }
        }
        return false;
    }

    public void create() {
        if (Platform.getDefault().validateConnection()) {
            GroupDialog dialog = new GroupDialog(this);
            dialog.setGroup(null);
            UIUtils.openDialog(dialog);
            if (dialog.isApproved()) {
                GroupListModel model = (GroupListModel) getModel();
                model.add(dialog.getGroup());
                ListUtils.setSelectionAtEnd(this);
            }
        }
    }

    public void update() {
        Group group = getSelectedGroup();
        if (group != null && Platform.getDefault().validateConnection()) {
            GroupDialog dialog = new GroupDialog(this);
            dialog.setGroup(group);
            UIUtils.openDialog(dialog);
            if (dialog.isApproved()) {
                GroupListModel model = (GroupListModel) getModel();
                int index = model.replace(dialog.getGroup());
                ListUtils.setSelectionAt(this, index);
            }
        }
    }

    public void delete() {
        Group group = getSelectedGroup();
        if (group != null && Platform.getDefault().validateConnection()) {
            Platform pl = Platform.getDefault();
            LocaleManager lm = pl.getLocaleManager();
            String msg = lm.getMessage("General.deleteMsg", group.getName());
            if (UIUtils.askConfirmation(this, msg)) {
                try {
                    boolean deleted = pl.getFacade().deleteGroup(pl.getCredentials(), group);
                    if (deleted) {
                        GroupListModel model = (GroupListModel) getModel();
                        model.remove(group);
                    } else {
                        refresh();
                        GroupListModel model = (GroupListModel) getModel();
                        int index = model.getIndexOf(group);
                        ListUtils.setSelectionAt(this, index);
                    }
                } catch (Exception ex) {
                    LoggerUtils.manageException(this, ex);
                }
            }
        }
    }

    public Group getSelectedGroup() {
        GroupListModel model = (GroupListModel) getModel();
        return model.getObjectAt(getSelectedIndex());
    }

    public List<Group> getSelectedGroups() {
        GroupListModel model = (GroupListModel) getModel();
        return model.getObjectsAt(getSelectedIndices());
    }

    class GroupListModel extends ListModel<Group> {
    }

    class GroupRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Group) {
                Group group = (Group) value;
                setText(group.getName());
                if (group.getDescription() == null || group.getDescription().trim().length() == 0) {
                    setToolTipText(group.getName());
                } else {
                    setToolTipText(group.getDescription());
                }
                setIcon(UIUtils.getIcon16x16("group.png"));
            }
            return this;
        }

    }

}
